package main

import (
	"2212b/awesome5Project12/api"
	"2212b/awesome5Project12/api/handler"
	"2212b/awesome5Project12/config"
	"2212b/awesome5Project12/inits"
	"2212b/awesome5Project12/queue"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
)

func main() {
	config.InitsConfig()
	inits.InitMysql()
	inits.InitRabbitMq()
	router := gin.Default()
	api.ToadRouter(router)
	go RabbitMq()
	router.Run(":9999") // 监听并在 0.0.0.0:8080 上启动服务
}
func RabbitMq() {
	inits.InitRabbitMq()

	msgs := queue.PublishSub("admin_send_all")

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)
			var body string
			json.Unmarshal(d.Body, &body)
			for _, onlineConn := range handler.OnlineUser {

				handler.WSRespSuccess(onlineConn, body)

			}
		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
